package com.westar.base.businessUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.westar.core.conf.SysConst;
import com.westar.base.util.WestarHttpUtil;

import java.io.IOException;
import java.util.Map;

/**
 * 工作流信息工具类
 */
public class WorkflowUtil {
    /**
     * 完成任务
     * @param actCookie 访问请求的标识
     * @param formParam 表单数据
     * @return 是否操作成功
     * @throws IOException
     */
    public static boolean completeTask(String actCookie, Map<String,String> formParam)
            throws IOException {
        String workflowURL = "http://"+ SysConst.DOMAIN+"/dothingsui/wftask/completeTask";
        String responBody = WestarHttpUtil.postMethod(workflowURL,actCookie,formParam);
        JSONObject jsonObject = JSON.parseObject(responBody);
        if(!jsonObject.isEmpty()){
           return true;
        }else{
            return false;
        }
    }
    /**
     * 回退、结束流程
     * @param actCookie 访问请求的标识
     * @param formParam 表单数据
     * @return
     */
    public static boolean jumpTask(String actCookie, Map<String,String> formParam) throws IOException {
        String workflowURL = "http://"+ SysConst.DOMAIN+"/dothingsui/processinst/jumpTask";
        String responBody = WestarHttpUtil.postMethod(workflowURL,actCookie,formParam);
        JSONObject jsonObject = JSONObject.parseObject(responBody);
        return jsonObject.getBoolean("success");

    }
    /**
     * 创建并启动流程
     * @param actCookie 访问请求的标识
     * @param formParam 表单数据
     * @return
     * @throws IOException
     */
    public static String createAndStartUpProcess(String actCookie, Map<String, String> formParam) throws IOException {
        String workflowURL = "http://"+ SysConst.DOMAIN + "/dothingsui/processinst/startProcessInst";
        String responeBody = WestarHttpUtil.postMethod(workflowURL,actCookie,formParam);
        return responeBody;
    }

    /**
     * 校验流程
     * @param actCookie 访问请求的标识
     * @param procDefKey 流程定义的Key
     * @return
     */
    public static String validDefByKey(String actCookie,String procDefKey) throws IOException {
        String workflowURL = "http://"+ SysConst.DOMAIN + "/dothingsui/wfProcDefValid/getByKey/"+procDefKey;
        String responeBody = WestarHttpUtil.getMethod(workflowURL,actCookie,null);
        return responeBody;

    }
}
